<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="dataForm.title" placeholder="标题"></el-input>
      </el-form-item>
      <el-form-item label="url链接地址" prop="url">
        <el-input v-model="dataForm.url" placeholder="url链接地址"></el-input>
      </el-form-item>
      <el-form-item label="图文代码" prop="imgUrl">
        <el-input v-model="dataForm.imgUrl" placeholder="图文代码"></el-input>
      </el-form-item>
      <div v-show="wenzivisible">
      <el-form-item label="文字颜色" prop="fontColor">
        <el-color-picker v-model="dataForm.fontColor" ></el-color-picker>
      </el-form-item>
      <el-form-item label="边框颜色" prop="boldColor">
        <el-color-picker v-model="dataForm.boldColor" ></el-color-picker>
      </el-form-item>
      <el-form-item label="效果" >
        <el-checkbox v-model="dataForm.fontExpShan">闪烁</el-checkbox>        <el-checkbox v-model="dataForm.fontExpBian">边框</el-checkbox>
      </el-form-item>
      <el-form-item label="字体大小" prop="fontSize">
        <el-input v-model="dataForm.fontSize" placeholder="字体大小"></el-input>
      </el-form-item>
      </div>
      <el-form-item label="状态" prop="status">
        <el-select v-model="dataForm.status" placeholder="请选择">
          <el-option
            v-for="item in statuss"
            :key="item.status"
            :label="item.label"
            :value="item.status">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="广告类型" prop="type">
        <el-select v-model="dataForm.type" placeholder="请选择" @change="wenzivisibleF(dataForm.type)">
          <el-option
            v-for="item in types"
            :key="item.type"
            :label="item.label"
            :value="item.type">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="广告位置" prop="type">
        <el-select v-model="dataForm.loca" placeholder="请选择">
          <el-option
            v-for="item in locas"
            :key="item.loca"
            :label="item.label"
            :value="item.loca">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item  label="排序号" prop="orderNum">
        <el-input-number v-model="dataForm.orderNum" controls-position="right" :min="1" label="排序号"></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import API from '@/api'
  export default {
    data () {
      return {
        visible: false,
        wenzivisible: false,
        statuss: [{
          status: 0,
          label: '关闭'
        }, {
          status: 1,
          label: '正常'
        }],
        types: [{
          type: 1,
          label: '代码'
        }, {
          type: 2,
          label: '图片'
        }, {
          type: 3,
          label: '文字'
        }, {
          type: 4,
          label: '退弹'
        }],
        locas: [{
          loca: 1,
          label: '顶部'
        }, {
          loca: 2,
          label: '浮动'
        }, {
          loca: 3,
          label: '中间'
        }, {
          loca: 4,
          label: '底部'
        }],
        dataForm: {
          id: 0,
          title: '',
          url: '',
          imgUrl: '',
          fontColor: '#00ff00',
          boldColor: '#ffffff',
          fontExpShan: false,
          fontExpBian: false,
          fontSize: '18',
          status: '1',
          createUserId: '',
          createTime: '',
          type: '',
          loca: '',
          orderNum: 1
        },
        dataRule: {
          imgUrl: [
            { required: true, message: '图文代码不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '广告类型不能为空', trigger: 'blur' }
          ],
          loca: [
            { required: true, message: '广告位置不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            API.baseadv.info(this.dataForm.id).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.baseadv.title
                this.dataForm.url = data.baseadv.url
                this.dataForm.imgUrl = data.baseadv.imgUrl
                this.dataForm.status = data.baseadv.status
                this.dataForm.fontColor = data.baseadv.fontColor
                this.dataForm.boldColor = data.baseadv.boldColor
                this.dataForm.fontExpShan = data.baseadv.fontExpShan
                this.dataForm.fontExpBian = data.baseadv.fontExpBian
                this.dataForm.fontSize = data.baseadv.fontSize
                this.dataForm.createUserId = data.baseadv.createUserId
                this.dataForm.createTime = data.baseadv.createTime
                this.dataForm.type = data.baseadv.type
                if (this.dataForm.type === 3) {
                  this.wenzivisible = true
                } else {
                  this.wenzivisible = false
                }
                this.dataForm.loca = data.baseadv.loca
                this.dataForm.orderNum = data.baseadv.orderNum
              }
            })
          }
        })
      },
      wenzivisibleF (value) {
        console.log(this.dataForm.fontColor)
        if (value === 3) {
          this.wenzivisible = true
        } else {
          this.wenzivisible = false
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
              'id': this.dataForm.id || undefined,
              'title': this.dataForm.title,
              'url': this.dataForm.url,
              'imgUrl': this.dataForm.imgUrl,
              'fontColor': this.dataForm.fontColor,
              'boldColor': this.dataForm.boldColor,
              'fontExpShan': this.dataForm.fontExpShan === true ? 1 : 0,
              'fontExpBian': this.dataForm.fontExpBian === true ? 1 : 0,
              'fontSize': this.dataForm.fontSize,
              'status': this.dataForm.status,
              'createUserId': this.dataForm.createUserId,
              'createTime': this.dataForm.createTime,
              'type': this.dataForm.type,
              'loca': this.dataForm.loca,
              'orderNum': this.dataForm.orderNum
            }
            var tick = !this.dataForm.id ? API.baseadv.add(params) : API.baseadv.update(params)
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
